import { defineStore } from "pinia";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import { Reactive, reactive, Ref, ref } from "vue";
import { get, post, put ,del} from "@/request";
export const useTaskStore = defineStore("Unit", () => {
    const search_obj_task = ref([
        { label: "名称", value: "", type: 'input' },
        { label: "编号", value: "", type: 'input' },
        { label: "项目类型", value: "", type: 'select' },
        { label: "开始时间", value: "", type: 'date' },
        { label: "负责人", value: "", type: 'input' },
        { label: "执行人", value: "", type: 'input' }
    ])
    const columns_show = ref([
        { label: "index", value: true },
        { label: "申请人", value: false },
        { label: "申请人部门", value: false },
        { label: "单号", value: false },
        { label: "名称", value: true },
        { label: "编号", value: true },
        { label: "项目类型", value: true },
        { label: "开始时间", value: true },
        { label: "预计结束时间", value: true },
        { label: "负责人", value: false },
        { label: "执行人", value: false },
        { label: "进度", value: true },
        { label: "内容", value: true },
        { label: "任务数", value: true },
        { label: "状态", value: true },
        { label: "操作列", value: true },

    ])
    const unfinished_list=ref([])
    const get_units = async (options = {}) => {
    };
    const list = ref([])
    const search_Val = reactive(
        {
            name: "",
            card: "",
            type: "",
            start_time: "",
            end_time: "",
            user: "",
            executor: "",
            status: ""
        }
    )
    const search_task = () => {
        if (search_Val.start_time) {
            search_Val.start_time = new Date(search_Val.start_time).getTime()
        }
        if (search_Val.end_time) {
            search_Val.end_time = new Date(search_Val.end_time).getTime()
        }
        getTask(search_Val)
    }
    const on_up_search = async () => {
        await get_units({
            name: search_obj[0].value,
            address: search_obj[1].value
        })
    }
    const length = ref(0)
    const getTask = async (options) => {
        const { data } = await get("/tasks/get_task", options);
        data.forEach((item) => {
            item.start_time = dayjs(item.start_time).format("YYYY-MM-DD HH:mm:ss");
            item.end_time = dayjs(item.end_time).format("YYYY-MM-DD HH:mm:ss");
        });
        list.value = data;
        length.value= data.length;
        unfinished_list.value = data.filter((item) => item.status == 2);
        console.log(unfinished_list.value);
        
    };
    const updateStatus = async (id: any, status: any) => {
        console.log(id, status);
        const { code } = await put("/tasks/updateStatus", {
            id: id,
            status: status,
        });
        if (code == 200) {
            ElMessage.success("更新成功");
            taskStore.getTask();
        }
    };
    const update_task_progress = async (id, progress) => {
        const { code } = await put("/tasks/updateProgress", { id, progress });
        if (code == 200) {
            ElMessage.success("更新成功");
            getTask(search_Val)
        }
    }
    let currentPage = ref(1)
    let pageSize = ref(4)
    const pageNum = computed(() => {
        return Math.ceil(length.value / pageSize.value);
      });

    
    
    const pre_page = (i: number) => {
        if (i == 1) {
            currentPage.value = 1
        } else {
            currentPage.value -= 1
        }
    }
    const next_page = (i: number) => {
        if (i == 1) {
            currentPage.value += 1
        } else {
            currentPage.value == -1
        }
    }

    const newlist = computed(() => {
        let start = (currentPage.value - 1) * pageSize.value
        let end = currentPage.value * pageSize.value
        return list.value.slice(start, end)
    })
    onMounted(async () => {
        await getTask()
    })
    const is_loading = ref(false)
    const loading = (val: boolean) => {
        is_loading.value = val
        setTimeout(() => {
            is_loading.value = false
        }, 500)
    }
    const del_task = async (id: number) => {
        let{code}=await del('/tasks/deleteTask',{id})
        if(code==200){
            ElMessage.success("删除成功");
            await getTask()
        }
    }
    return {
        search_obj_task,
        search_task,
        search_Val,
        getTask,
        list,
        columns_show,
        update_task_progress,
        currentPage,
        pre_page,
        next_page,
        is_loading,
        loading,
        newlist,
        length,
        pageNum,
        pageSize,
        del_task,
        updateStatus,
        unfinished_list
    };
});